package com.example.tivi.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;

import android.content.Context;
import android.util.Log;

import com.example.tivi.entity.GlobalInfor;

public class Data {
	public static void saveData(Context context, GlobalInfor globalInfor) {
		try {
			FileOutputStream fos = context.openFileOutput("dt",
					Context.MODE_PRIVATE);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(globalInfor);
			oos.flush();
			oos.close();
			Log.i("Data", "save success");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			Log.e("Data", "save error");
		} catch (IOException e) {
			e.printStackTrace();
			Log.e("Data", "save error");
		}
	}

	public static GlobalInfor getData(Context context) {
		try {
			File file = context.getFileStreamPath("dt");
			FileInputStream fis = new FileInputStream(file);
			ObjectInputStream ois = new ObjectInputStream(fis);
			GlobalInfor globalInfor = (GlobalInfor) ois.readObject();
			ois.close();
			return globalInfor;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (StreamCorruptedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
}
